package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.model.entity.Examprojecttable;
import com.qf.dao.ExamprojecttableDao;
import com.qf.listener.ExamprojecttableListener;
import com.qf.service.ExamprojecttableService;
import com.qf.util.R;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Examprojecttable)表服务实现类
 *
 * @author makejava
 * @since 2024-05-01 09:31:29
 */
@Service("examprojecttableService")
public class ExamprojecttableServiceImpl implements ExamprojecttableService {
    @Autowired
    private ExamprojecttableDao examprojecttableDao;


    @Override
    public R importExamprojecttable(MultipartFile file) {


        // 1.读取excel 表格
        //将上传的excel  批量导入到数据库中

        List<Examprojecttable> list = null;
        try {
            list = EasyExcel.read(file.getInputStream(), Examprojecttable.class, new ExamprojecttableListener(this)).sheet("体检项目").doReadSync();

            System.out.println("批量导入数据  list.size() = " + list.size());

            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.fail("导入失败");

    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        return examprojecttableDao.deleteBatch(list);
    }

    @Override
    public List<Examprojecttable> examprojecttableByCondition(Map<String,Object> map) {
        return examprojecttableDao.examprojecttableByCondition(map);
    }

    /**
     *
     * 通过ID查询单条数据
     *
     * @param pid 主键
     * @return 实例对象
     */
    @Override
    public Examprojecttable queryById(Integer pid) {
        return this.examprojecttableDao.queryById(pid);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Examprojecttable> queryByPage(Integer pageNum) {
        return this.examprojecttableDao.queryAllByLimit();
    }

    /**
     * 新增数据
     *
     * @param examprojecttable 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Examprojecttable examprojecttable, HttpSession session) {
      int num =   this.examprojecttableDao.insert(examprojecttable);

      if (num>0){
          return true;
      }
        return false;
    }

    /**
     * 修改数据
     *
     * @param examprojecttable 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Examprojecttable examprojecttable) {

      int num =   this.examprojecttableDao.update(examprojecttable);
      if (num>0){
          return true;
      }
      return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param pid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pid) {
        return this.examprojecttableDao.deleteById(pid) > 0;
    }


    @Override
    public void examprojecttableExport(HttpServletResponse response) {
        // 查询所有数据，因为数据库有查询多数据的 就用  先写listen
        List<Examprojecttable> examprojecttables = examprojecttableDao.queryAllByLimit();
        try {
            // 导出excel 表格



            // 2.导出excel 表格
            //  write(response.getOutputStream()  响应的输出流,Student.class  指定表头)
            //   .sheet("xuesheng") 指定 excel 文件中那个表
            //   .doWrite(allStudent)写list 数组
            EasyExcel.write(response.getOutputStream(),Examprojecttable.class).sheet("体检项目").doWrite(examprojecttables);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void insertBatch(List<Examprojecttable> examprojecttableList) {
        examprojecttableDao.insertBatch(examprojecttableList);
    }
}
